describe("api_accountHistory in SG", function() {
	var account;
	var callback;
	
    beforeEach(function() {
        App.initSession();
        
        account = {};
        callback = jasmine.createSpy();
        App.api_accountHistory.callbackAccountHistory = callback;
    });

    describe("_buildTransactionList", function() {

        it("build Transaction List by account", function() {
            var transactionList = [{
            	"runningBalance": 1,
            	"toAccountNumber": "1111",
            	"withdrawalAmount": 100,
            	"amount": 100,
            	"transactionDescription": "1111",
            	"postDate": "2011-10-01T00:00:00.000+0530",
            	"transactionCurrency": "SGD"
            },
            {
            	"runningBalance": 2,
            	"toAccountNumber": "2222",
            	"withdrawalAmount": 101,
            	"amount": 101,
            	"transactionDescription": "2222",
            	"postDate": "2011-10-02T00:00:00.000+0530",
            	"transactionType": "D",
            	"transactionCurrency": "SGD"
            },
            {
            	"runningBalance": 3,
            	"toAccountNumber": "3333",
            	"withdrawalAmount": 103,
            	"amount": 103,
            	"transactionDescription": "3333",
            	"postDate": "2011-10-02T00:00:00.000+0530",
            	"transactionType": "D",
            	"transactionCurrency": "SGD"
            }];
            var account = App.BaseAccount.create();

            var transactionArray = App.api_accountHistory._buildTransactionList(transactionList, account);

            expect(transactionArray['2011-10-01T00:00:00.000+0530'].length).toEqual(1);
        });
        
        it("build Transaction List by card", function() {
        	var transactionList = [{
            	"runningBalance": 1,
            	"toAccountNumber": "1111",
            	"withdrawalAmount": 100,
            	"amount": 100,
            	"transactionDescription": "1111",
            	"trxnDate": "2011-10-01T00:00:00.000+0530",
            	"transactionCurrency": "SGD"
            },
            {
            	"runningBalance": 2,
            	"toAccountNumber": "2222",
            	"withdrawalAmount": 101,
            	"amount": 101,
            	"transactionDescription": "2222",
            	"trxnDate": "2011-10-02T00:00:00.000+0530",
            	"transactionType": "D",
            	"transactionCurrency": "SGD"
            },
            {
            	"runningBalance": 3,
            	"toAccountNumber": "3333",
            	"withdrawalAmount": 103,
            	"amount": 103,
            	"transactionDescription": "3333",
            	"postDate": "2011-10-02T00:00:00.000+0530",
            	"transactionType": "D",
            	"transactionCurrency": "SGD"
            }];
            var account = App.CardAccount.create();

            var transactionArray = App.api_accountHistory._buildTransactionList(transactionList, account);
            
            expect(transactionArray['2011-10-02T00:00:00.000+0530'].length).toEqual(1);
        });

    });
    
    describe("_processAccountHistory", function() {

        it("get account history is success and accountHistoryList is null", function() {
            var response = {
                accountHistoryResponse: {
                    accountHistoryList: [],
                    responseHeader: {
                        status: 1,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalledWith(null);
        });

        it("get account history is success and accountHistoryList have a null item", function() {
            var response = {
                accountHistoryResponse: {
                    accountHistoryList: [null],
                    responseHeader: {
                        status: 1,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }
            spyOn(App.api_accountHistory, '_buildTransactionList');

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory._buildTransactionList).not.toHaveBeenCalled();
            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalledWith([], null);
        });
        
        it("get account history is success and accountHistoryList have a item", function() {
            var response = {
                accountHistoryResponse: {
                    accountHistoryList: [{
                        transactionList: [{}]
                    }],
                    responseHeader: {
                        status: 1,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }
            spyOn(App.api_accountHistory, '_buildTransactionList').andReturn([{
                id: '1'
            }]);

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory._buildTransactionList).toHaveBeenCalledWith(response.accountHistoryResponse.accountHistoryList[0].transactionList, account);
            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalledWith([{
                id: '1'
            }], null);
        });

        it("get account history is success and transactionList is null and transactionsForUnitTrustList is not null", function() {
            var response = {
                accountHistoryResponse: {
                    accountHistoryList: [{
                        transactionList: [],
                        transactionsForUnitTrustList: [{}]
                    }],
                    responseHeader: {
                        status: 1,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }
            var account = {};
            var callback = jasmine.createSpy();
            App.api_accountHistory.callbackAccountHistory = callback;
            spyOn(App.api_accountHistory, '_buildTransactionList').andReturn([{
                id: '1'
            }]);

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory._buildTransactionList).toHaveBeenCalledWith(response.accountHistoryResponse.accountHistoryList[0].transactionsForUnitTrustList, account);
            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalledWith([{
                id: '1'
            }], null);
        });

        it("get account history is success and transactionList is null and transactionsForUnitTrustList is null", function() {
            var response = {
                accountHistoryResponse: {
                    accountHistoryList: [{
                        transactionList: [],
                        transactionsForUnitTrustList: []
                    }],
                    responseHeader: {
                        status: 1,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }
            var account = {};
            var callback = jasmine.createSpy();
            App.api_accountHistory.callbackAccountHistory = callback;
            spyOn(App.api_accountHistory, '_buildTransactionList');

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory._buildTransactionList).not.toHaveBeenCalled();
            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalledWith([], null);
        });
        
        it("get account history is error", function() {
            var response = {
                accountHistoryResponse: {
                    responseHeader: {
                        status: 0,
                        currentTimestamp: "2011-10-01T18:58:31.585+0530",
                        nonce: "a08b0eff0b7b34a611b2e529c20a7351"
                    }
                }
            }
            var account = {};
            var callback = jasmine.createSpy();
            App.api_accountHistory.callbackAccountHistory = callback;
            spyOn(App.api_accountHistory, '_buildTransactionList');

            App.api_accountHistory._processAccountHistory(response, account);

            expect(App.api_accountHistory._buildTransactionList).not.toHaveBeenCalled();
            expect(App.api_accountHistory.callbackAccountHistory).toHaveBeenCalled();
        });
    });

});